﻿using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.CustomControls;
using JiveMessenger.Interfaces;
using JiveMessenger.Models;
using JiveMessenger.Settings;
using JiveMessenger.User;
using JmShared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JiveMessenger.ViewModels
{
    public class ViewModelPrivateMessage : ViewModelBaseChatControl, IViewModelChat
    {
        public RelayCommand EmoticonPopupBackspaceCommand { get; private set; }
        public RelayCommand<EmoticonClickedEventArgs> EmoticonPopupClickEmoticonCommand { get; private set; }
        public RelayCommand<string> EmoticonPopupSendMessageCommand { get; private set; }
        public RelayCommand<SendMessageEventArgs> SendMessageCommand { get; private set; }
        public RelayCommand<System.Windows.Input.GestureEventArgs> ChatControlWindowTappedCommand { get; private set; }

        public string ID { get; set; }
        public string ViewModelName { get; set; }

        private bool _showKeyboard;

        public bool ShowKeyboard
        {
            get { return _showKeyboard; }
            set
            {
                if (_showKeyboard != value)
                {
                    _showKeyboard = value;
                    RaisePropertyChanged("ShowKeyboard");
                }
            }
        }

        private bool _showFauxKeyboardGrid;

        public bool ShowFauxKeyboardGrid
        {
            get { return _showFauxKeyboardGrid; }
            set
            {
                if (_showFauxKeyboardGrid != value)
                {
                    _showFauxKeyboardGrid = value;
                    RaisePropertyChanged("ShowFauxKeyboardGrid");
                }
            }
        }
        public ViewModelPrivateMessage()
        {
            EmoticonPopupSendMessageCommand = new RelayCommand<string>(message => EmoticonPopupSendMessageCommandAction(message), message => Client.Instance.IsLoggedIn);
            EmoticonPopupBackspaceCommand = new RelayCommand(EmoticonPopupBackspaceCommandAction);
            EmoticonPopupClickEmoticonCommand = new RelayCommand<EmoticonClickedEventArgs>(EmoticonPopupClickEmoticonCommandAction);
            SendMessageCommand = new RelayCommand<SendMessageEventArgs>(SendMessageCommandAction);
            ChatControlWindowTappedCommand = new RelayCommand<System.Windows.Input.GestureEventArgs>(ChatControlWindowTappedCommandAction);
        }
        private void EmoticonPopupBackspaceCommandAction()
        {
            int index = this.InputTextSelectionStart - 1;
            if (index > 0)
            {
                this.InputText = InputText.Remove(index, 1);
                this.InputTextSelectionStart = index;
                this.InputTextSelectionLength = 0;
            }
            else
                this.InputText = string.Empty;
        }
        private void ChatControlWindowTappedCommandAction(System.Windows.Input.GestureEventArgs args)
        {
            this.ShowEmoticonPopup = false;

            //ShowConnectPopup = false;
            ShowKeyboard = false;
        }
        private void EmoticonPopupClickEmoticonCommandAction(EmoticonClickedEventArgs args)
        {
            EmoticonModel model = args.Model;
            string key = string.Empty;

            if (model.EmoticonType == EmoticonType.CustomEmoticon)
            {
                key = model.EmoticonShortcut.ToString();
                if (!AppSettings.AppSettingsInstance.RecentlyUsedCustomEmoticons.ContainsKey(key))
                    AppSettings.AppSettingsInstance.RecentlyUsedCustomEmoticons.Add(key, 0);
                AppSettings.AppSettingsInstance.RecentlyUsedCustomEmoticons[key]++;
            }
            else if (model.EmoticonType == EmoticonType.DefaultEmoticon)
            {
                string[] keyArray = model.EmoticonShortcut as string[];
                key = keyArray[0];
                if (!AppSettings.AppSettingsInstance.RecentlyUsedDefaultEmoticons.ContainsKey(keyArray))
                    AppSettings.AppSettingsInstance.RecentlyUsedDefaultEmoticons.Add(keyArray, 0);
                AppSettings.AppSettingsInstance.RecentlyUsedDefaultEmoticons[keyArray]++;

            }
            UpdateRecentlyUsedEmoticonList(model);
            InsertEmoticon(key);

        }
        private void EmoticonPopupSendMessageCommandAction(string messageText)
        {
            SendMessage(messageText);
            ShowEmoticonPopup = false;
            ShowKeyboard = true;
        }

        private void SendMessageCommandAction(SendMessageEventArgs args)
        {
            SendMessage(args.Text);
        }
        private void InsertEmoticon(string key)
        {
            int offset = InputTextSelectionStart;
            InputTextSelectionLength = 0;
            InputText = InputText.Insert(offset, key);
            InputTextSelectionStart = offset + key.Length;
        }
        public void SendMessage(string text)
        {
            if (Client.Instance.IsLoggedIn)
            {
                MessageType mt = MessageType.PrivateMessage;
                DisplayMessage(Client.Instance.CurrentUserInfo, text, DateTime.Now);
                ChatMessage msg = new ChatMessage(Client.Instance.CurrentUserInfo.UserID, mt)
                {
                    Message = text,
                    MessageRelayTime = DateTime.Now,
                     RecipientID = this.ID
                };

                if (!App.Connection.SendMessage(msg))
                {
                    DisplayMessage(Client.RedText, String.Format("Could not deliver message: \r\n{0}", msg.Message), DateTime.Now);
                }
            }
            else
            {
                Messenger.Default.Send("Not logged in");
            }
            this.InputText = string.Empty;
        }
    }
}
